package com.fub.guidance;

import java.util.ArrayList;
import java.util.List;
import com.fub.util.database.databaseHelper.WallsDatabaseManager;

import android.content.Context;
import android.database.Cursor;

public class BuildingDAO {

	private static final int BUILDING_ID_POSITION = 1;
	private static final int X1_POSITION = 2;
	private static final int Y1_POSITION = 3;
	private static final int X2_POSITION = 4;
	private static final int Y2_POSITION = 5;
	private static final int Z_POSITION = 6;

	private WallsDatabaseManager wallsDatabaseManager;

	/**
	 * Constructs a new data access object for building data.
	 * 
	 * @param context
	 *            the application context
	 */
	public BuildingDAO(Context context) {
		this.wallsDatabaseManager = new WallsDatabaseManager(context);
	}

	/**
	 * Insert a wall into database.
	 * 
	 * @param wall
	 *            the wall to be insert.
	 */
	public void insertWall(Wall wall) {
		String query = String
				.format(" INSERT INTO %s (%s,%s,%s,%s,%s,%s) VALUES ('%s','%s','%s','%s','%s','%s')",
						WallsDatabaseManager.TABLE_NAME_WALLS,
						WallsDatabaseManager.BUILDING_ID_COL,
						WallsDatabaseManager.COL_X1,
						WallsDatabaseManager.COL_Y1,
						WallsDatabaseManager.COL_X2,
						WallsDatabaseManager.COL_Y2L,
						WallsDatabaseManager.COL_Z, wall.getBuildingId(),
						wall.getX1(), wall.getY1(), wall.getX2(), wall.getY2(),
						wall.getZ());
		this.wallsDatabaseManager.getWritableDatabase().execSQL(query);
	}

	/**
	 * Returns the walls of a building.
	 * 
	 * @param buildingId
	 *            the building id.
	 * @return the walls of a building.
	 */
	public List<Wall> getWalls(String buildingId) {
		List<Wall> result = new ArrayList<Wall>();
		String query = String.format("Select * FROM %s WHERE %s = '%s'",
				WallsDatabaseManager.TABLE_NAME_WALLS,
				WallsDatabaseManager.BUILDING_ID_COL, buildingId);
		Cursor wallCursor = this.wallsDatabaseManager.getReadableDatabase()
				.rawQuery(query, null);
		while (wallCursor.moveToNext()) {
			String bId = wallCursor.getString(BUILDING_ID_POSITION);
			int x1 = wallCursor.getInt(X1_POSITION);
			int y1 = wallCursor.getInt(Y1_POSITION);
			int x2 = wallCursor.getInt(X2_POSITION);
			int y2 = wallCursor.getInt(Y2_POSITION);
			int z = wallCursor.getInt(Z_POSITION);
			result.add(new Wall(bId, x1, y1, x2, y2, z));
		}
		wallCursor.close();
		return result;
	}

	/**
	 * Returns the walls of all building.
	 * 
	 * @return the walls of all building.
	 */
	public List<Wall> getAllWalls() {
		List<Wall> result = new ArrayList<Wall>();
		String query = String.format("SELECT * FROM %s",
				WallsDatabaseManager.TABLE_NAME_WALLS);
		Cursor wallCursor = this.wallsDatabaseManager.getReadableDatabase()
				.rawQuery(query, null);
		while (wallCursor.moveToNext()) {
			String buildingId = wallCursor.getString(BUILDING_ID_POSITION);
			int x1 = wallCursor.getInt(X1_POSITION);
			int y1 = wallCursor.getInt(Y1_POSITION);
			int x2 = wallCursor.getInt(X2_POSITION);
			int y2 = wallCursor.getInt(Y2_POSITION);
			int z = wallCursor.getInt(Z_POSITION);
			result.add(new Wall(buildingId, x1, y1, x2, y2, z));
		}
		wallCursor.close();
		return result;
	}

	/**
	 * Deletes all walls of a building.
	 * 
	 * @param buildingId
	 *            the building id.
	 * @return the walls of the building.
	 */

	public void deleteAllWalls(String buildingId) {
		String query = String.format("DELETE FROM %s WHERE %s = '%s'",
				WallsDatabaseManager.TABLE_NAME_WALLS,
				WallsDatabaseManager.BUILDING_ID_COL, buildingId);
		this.wallsDatabaseManager.getWritableDatabase().execSQL(query);
	}

	/**
	 * Deletes all walls of all buildings.
	 * 
	 */
	public void deleteAllWalls() {
		String query = String.format("DELETE FROM %s ",
				WallsDatabaseManager.TABLE_NAME_WALLS);
		this.wallsDatabaseManager.getWritableDatabase().execSQL(query);
	}
}
